﻿using AutoMapper;
using DevProtocolApi.Dtos.system;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.EntityModel.Auth;
using Model.EntityModel.SysModel;
using Model.QueryModel.system;
using Service.IService.ISysService;

namespace DevProtocolApi.Controllers.sysControllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GroupMasterController : BaseController
    {
        private readonly IGroupService _groupService;
        private readonly ILogger<GroupMasterController> _logger;
        private readonly IMapper _mapper;

        public GroupMasterController(
            IGroupService groupService,
            ILogger<GroupMasterController> logger,
            IMapper mapper)
        {
            _groupService = groupService;
            _logger = logger;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取所有有效组别
        /// </summary>
        [HttpGet]
        public IActionResult GetAll()
        {
            try
            {
                var groups = _groupService.GetActiveGroups();
                return Success(groups);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取组别列表失败");
                return Failed($"获取失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取所有有效组别
        /// </summary>
        [HttpPost]
        public IActionResult GetPageGroup(int pageNum, int pageSize, QueryGroup search)
        {
            try
            {
                
                var groups = _groupService.GetPageGroup(1, 15, search);
                return Success(groups);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取组别列表失败");
                return Failed($"获取失败: {ex.Message}");
            }
        }
        /// <summary>
        /// 创建新组别
        /// </summary>
        [HttpPost]
        public IActionResult Create([FromBody] GroupMasterDto dto)
        {
            try
            {
                var group = _mapper.Map<Group>(dto);
                bool success = _groupService.CreateGroup(group);
                return success
                    ? Success("组别创建成功")
                    : Failed("组别创建失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"创建组别失败: {dto.Name}");
                return Failed($"创建失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 更新组别信息
        /// </summary>
        [HttpPut]
        public IActionResult Update([FromBody] GroupMasterDto dto)
        {
            try
            {
                var group = _mapper.Map<Group>(dto);
                bool success = _groupService.UpdateGroup(group);
                return success
                    ? Success("组别更新成功")
                    : Failed("组别更新失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"更新组别失败: {dto.Id}");
                return Failed($"更新失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 更新组别状态
        /// </summary>
        [HttpPost("status/{groupId}")]
        public IActionResult UpdateStatus(string groupId, [FromBody] string isActive)
        {
            try
            {
                bool success = _groupService.UpdateGroupStatus(groupId, isActive);
                return success
                    ? Success("组别状态更新成功")
                    : Failed("组别状态更新失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"更新组别状态失败: {groupId}");
                return Failed($"更新失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取组别详情
        /// </summary>
        [HttpGet("{groupId}")]
        public IActionResult GetById(string groupId)
        {
            try
            {
                var group = _groupService.GetGroupById(groupId);
                return group != null
                    ? Success(group)
                    : Failed("组别不存在");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取组别信息失败: {groupId}");
                return Failed($"获取失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 添加用户到组别
        /// </summary>
        /// <param name="groupId">组别ID</param>
        /// <param name="userIds">用户ID列表</param>
        /// <returns>操作结果</returns>
        [HttpPost("users/{groupId}")]
        public IActionResult AddUsers(string groupId, [FromBody] List<string> userIds)
        {
            try
            {
                bool success = _groupService.AddUsersToGroup(groupId, userIds);
                return success
                    ? Success("添加用户成功")
                    : Failed("添加用户失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加用户到组别失败: {groupId}");
                return Failed($"添加失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 从组别移除用户
        /// </summary>
        /// <param name="groupId">组别ID</param>
        /// <param name="userIds">用户ID列表</param>
        /// <returns>操作结果</returns>
        [HttpDelete("users/{groupId}")]
        public IActionResult RemoveUsers(string groupId, [FromBody] List<string> userIds)
        {
            try
            {
                bool success = _groupService.RemoveUsersFromGroup(groupId, userIds);
                return success
                    ? Success("移除用户成功")
                    : Failed("移除用户失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"从组别移除用户失败: {groupId}");
                return Failed($"移除失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取组别关联的用户
        /// </summary>
        /// <param name="groupId">组别ID</param>
        /// <returns>用户列表</returns>
        [HttpGet("users/{groupId}")]
        public IActionResult GetGroupUsers(string groupId)
        {
            try
            {
                var users = _groupService.GetGroupUsers(groupId);
                return Success(users);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取组别用户失败: {groupId}");
                return Failed($"获取失败: {ex.Message}");
            }
        }
    }
}